import request from "@/axios/config";
import axios from "axios";
import {appUrl} from "@/axios/env";
import qs from "qs";

//post -- json
export function postJsonAction(url,parameter) {
  return request({
    url: appUrl + url,
    method:'post' ,
    data: parameter
  })
}

//delete -- json
export function delJsonAction(url,parameter) {
  return request({
    url: appUrl + url,
    method:'delete' ,
    params: parameter
  })
}

//post -- formdata
export function postFormdataAction(url,parameter) {
  return request({
    url: appUrl + url,
    method:'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(parameter)
  })
}

//get
export function getAction(url,parameter) {
  return request({
    url: appUrl + url,
    method: 'get',
    params: parameter
  })
}
// get all
export function requestAll(arr) {
  return axios.all(arr).then(axios.spread((...res) => Promise.resolve(res)))
}

// 导出
export function exportAction(url,parameter) {
  axios.get(appUrl + url, {
    responseType: 'blob',
    headers: {
      // 'Content-Type': 'application/json',
      // 'X-Access-Token': getToken()
    },
    params: parameter
  }).then(res => {

    const blob = new Blob([res.data], {
      type: res.data.type
    })
    const objectUrl = URL.createObjectURL(blob)
    let a = document.createElement("a");
    a.href = objectUrl;
    a.download = "统计.xls" // 就是指定的文件名
    a.click()
    if(a.parentNode!=null){
      a.parentNode.removeChild(a);
    }

  }).catch(err => {
    console.log(err)
  })
}

// 下载
export function downloadAction(url,parameter) {
  axios.get(appUrl + url + '?id=' + parameter.id, {
    responseType: 'blob',
    headers: {
      // 'Content-Type': 'application/json',
      // 'X-Access-Token': getToken()
    }
  }).then(res => {
    const blob = new Blob([res.data], {
      type: res.data.type
    })
    const objectUrl = URL.createObjectURL(blob)
    let a = document.createElement("a");
    a.href = objectUrl;
    a.download = parameter.name // 就是指定的文件名
    a.click()
    if(a.parentNode!=null){
      a.parentNode.removeChild(a);
    }

  }).catch(err => {
    console.log(err)
  })
}

